import os,time
choose = int(input('请选择更换方式:\n1、自动（不支持WinXP系统）\n2、手动\n>>> '))
if choose == 1:
    system = os.name
    homeDir = os.getenv('HOME')
    if system == 'nt':
        try:
            os.mkdir(homeDir+'\\pip')
            os.mknod(homeDir+'\\pip\\pip.ini')
        except FileExistsError:
            pass
        iniFile = open(homeDir+'\\pip\\pip.ini')
        print('当前操作系统：Windows\n用户主目录：'+homeDir+'pip配置文件：'+homeDir+'\\pip\\pip.ini')
        
        mirror = int(input('请选择镜像源：(输入序号选择)\n1 阿里云源\n2 清华源\n3 中科大源\n4 华工大源\n5 山工大源\n6 豆瓣源\n>>> '))

        if mirror == 1:
            print('[global]\nindex-url = http://mirrors.aliyun.com/pypi/simple/\n[install]\ntrusted-host = mirrors.aliyun.com',file=iniFile)
        if mirror == 2:
            print('[global]\nindex-url = https://pypi.tuna.tsinghua.edu.cn/simple/\n[install]\ntrusted-host = pypi.tuna.tsinghua.edu.cn',file=iniFile)
        if mirror == 3:
            print('[global]\nindex-url = https://pypi.mirrors.ustc.edu.cn/simple/\n[install]\ntrusted-host = pypi.mirrors.ustc.edu.cn',file=iniFile)
        if mirror == 4:
            print('[gloval]\nindex-url = http://pypi.hustunique.com/\n[install]\ntrusted-host = pypi.hustunique.com',file=iniFile)
        if mirror == 5:
            print('[global]\nindex-url = http://pypi.sdutlinux.org/\n[install]\ntrusted-host = pypi.sdutlinux.org',file=iniFile)
        if mirror == 6:
            print('[global]\nindex-url = http://pypi.douban.com/simple/\n[install]\ntrusted-host = pypi.douban.com',file=iniFile)
            
        iniFile.close()

        os.system('pip install pip --upgrade')
        os.system('pip3 install pip --upgrade')

    if system == 'posix':
        try:
            os.mkdir(homeDir+'/.pip')
            os.mknod(homeDir+'/.pip/pip.conf')
        except FileExistsError:
            pass
        cfgFile = open(homeDir+'/.pip/pip.conf','w')
        print('当前操作系统：Linux/UNIX\n用户主目录：'+homeDir+'\npip配置文件：'+homeDir+'/.pip/pip.conf')
        time.sleep(1)

        mirror = int(input('请选择镜像源：(输入序号选择)\n1 阿里云源\n2 清华源\n3 中科大源\n4 华工大源\n5 山工大源\n6 豆瓣源\n>>> '))

        if mirror == 1:
            print('[global]\nindex-url = https://mirrors.aliyun.com/pypi/simple/',file=cfgFile)
        if mirror == 2:
            print('[global]\nindex-url = https://pypi.tuna.tsinghua.edu.cn/simple/',file=cfgFile)
        if mirror == 3:
            print('[global]\nindex-url = https://pypi.mirrors.ustc.edu.cn/simple/',file=cfgFile)
        if mirror == 4:
            print('[gloval]\nindex-url = http://pypi.hustunique.com/',file=cfgFile)
        if mirror == 5:
            print('[global]\nindex-url = http://pypi.sdutlinux.org/',file=cfgFile)
        if mirror == 6:
            print('[global]\nindex-url = http://pypi.douban.com/simple/',file=cfgFile)
            
        cfgFile.close()
        
        print('正在更新镜像源，请稍候，请勿退出。')
        os.system('pip install pip --upgrade')
        os.system('pip3 install pip --upgrade')
        print('已完成，更换的镜像源为：'+mirror)

if choose == 2:
    system = int(input('请输入系统类型：\n1 Windows\n2 Linux/UNIX\n>>> '))
    homeDir = os.getenv('HOME')
    if system == 1:
        os.system('mkdir %USERPROFILE%\\pip')
        os.system('cd . > %USERPROFILE%\\pip\\pip.ini')
        iniFile = open(homeDir+'\\pip\\pip.ini','w')

        mirror = int(input('请选择镜像源：(输入序号选择)\n1 阿里云源\n2 清华源\n3 中科大源\n4 华工大源\n5 山工大源\n6 豆瓣源\n>>> '))

        if mirror == 1:
            print('[global]\nindex-url = http://mirrors.aliyun.com/pypi/simple/\n[install]\ntrusted-host = mirrors.aliyun.com',file=iniFile)
        if mirror == 2:
            print('[global]\nindex-url = https://pypi.tuna.tsinghua.edu.cn/simple/\n[install]\ntrusted-host = pypi.tuna.tsinghua.edu.cn',file=iniFile)
        if mirror == 3:
            print('[global]\nindex-url = https://pypi.mirrors.ustc.edu.cn/simple/\n[install]\ntrusted-host = pypi.mirrors.ustc.edu.cn',file=iniFile)
        if mirror == 4:
            print('[gloval]\nindex-url = http://pypi.hustunique.com/\n[install]\ntrusted-host = pypi.hustunique.com',file=iniFile)
        if mirror == 5:
            print('[global]\nindex-url = http://pypi.sdutlinux.org/\n[install]\ntrusted-host = pypi.sdutlinux.org',file=iniFile)
        if mirror == 6:
            print('[global]\nindex-url = http://pypi.douban.com/simple/\n[install]\ntrusted-host = pypi.douban.com',file=iniFile)
            
        iniFile.close()

        os.system('pip install pip --upgrade')
        os.system('pip3 install pip --upgrade')

    if system == 2:
        os.system('mkdir ~/.pip')
        os.system('touch ~/.pip/pip.conf')
        cfgFile = open(homeDir+'/.pip/pip.conf','w')

        mirror = int(input('请选择镜像源：(输入序号选择)\n1 阿里云源\n2 清华源\n3 中科大源\n4 华工大源\n5 山工大源\n6 豆瓣源\n>>> '))

        if mirror == 1:
            print('[global]\nindex-url = https://mirrors.aliyun.com/pypi/simple/',file=cfgFile)
        if mirror == 2:
            print('[global]\nindex-url = https://pypi.tuna.tsinghua.edu.cn/simple/',file=cfgFile)
        if mirror == 3:
            print('[global]\nindex-url = https://pypi.mirrors.ustc.edu.cn/simple/',file=cfgFile)
        if mirror == 4:
            print('[gloval]\nindex-url = http://pypi.hustunique.com/',file=cfgFile)
        if mirror == 5:
            print('[global]\nindex-url = http://pypi.sdutlinux.org/',file=cfgFile)
        if mirror == 6:
            print('[global]\nindex-url = http://pypi.douban.com/simple/',file=cfgFile)
            
        cfgFile.close()

        os.system('pip install pip --upgrade')
        os.system('pip3 install pip --upgrade')
